<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'sh:order:zh:page'"
        :excelPer="'sh:order:zh:export'"
        @search="search"
        @handleDataExport="handleDataExport"
        :showSearchButton="true"
        :showExportButton="true"
        @export="excelInfo"
      >
        <el-date-picker
          v-model="form.swopTime"
          type="datetimerange"
          start-placeholder="置换交车开始时间"
          end-placeholder="置换交车结束时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-select
          clearable
          style="width: 150px"
          v-model="form.carBrandId"
          placeholder="请选择品牌"
          @change="handleCarBrand"
        >
          <el-option
            v-for="item in allBrand"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.carSeriesId"
          placeholder="请选择车系"
        >
          <el-option
            v-for="item in allSeries"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
        <el-select
          clearable
          style="width: 150px"
          v-model="form.type"
          placeholder="置换类型"
        >
          <el-option
            v-for="item in getDict(['置换类型'])['置换类型']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-input
          v-model="form.client"
          clearable
          placeholder="客户名称/手机号"
          style="width: 150px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-search"
            title="查看"
            style="margin-left: 15px; cursor: pointer"
            v-permission="'sh:order:zh:detail'"
            @click="handleDetails(row)"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'sh:order:zh:delete'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import {
  getList,
  update,
  deleteById
} from '@/api/pre-sale/orderManagement/replacement'
import { getDict, exportExcel } from '@/utils/index'
import { getAllBrand, getSeries } from '@/api/after-sale/systemManage/carModel'

export default {
  mixins: [
    baseMixin({
      idName: 'id',
      findData: getList,
      updateData: update,
      deleteData: deleteById,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.type = data.type === 1 ? '置换补差价' : '提现'
          data.gearbox = data.gearbox === 1 ? '手动' : '自动'
          return data
        })
      }
    })
  ],

  data() {
    return {
      allBrand: [], // 品牌
      allSeries: [], // 车系
      getDict: getDict,
      form: {
        swopTime: [], // 客户信息
        carBrandId: '',
        carSeriesId: '',
        type: '',
        client: ''
      },
      tableColumnAttributes: [
        {
          name: '所属门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '品牌',
          value: 'carBrandName',
          type: 'text'
        },
        {
          name: '车系',
          value: 'carSeriesName',
          type: 'text'
        },
        {
          name: '车型',
          value: 'carModelName',
          type: 'text'
        },
        {
          name: '颜色',
          value: 'color',
          type: 'text'
        },
        {
          name: '置换类型',
          value: 'type',
          type: 'text'
        },
        {
          name: '置换价',
          value: 'replacementPrice',
          type: 'text'
        },
        {
          name: '表显里程',
          value: 'mileage',
          type: 'text'
        },
        {
          name: '首次上牌',
          value: 'firstLicenseTime',
          type: 'text'
        },
        {
          name: '排量',
          value: 'displacement',
          type: 'text'
        },
        {
          name: '变速箱',
          value: 'gearbox',
          type: 'text'
        },
        {
          name: '客户名称',
          value: 'clientName',
          type: 'text'
        },
        {
          name: '客户手机',
          value: 'clientPhone',
          type: 'text'
        }
      ],
      dict: {}
    }
  },
  methods: {
    async handleCarBrand(val) {
      val && await getSeries(val).then((res) => {
        const { success, data } = res
        if (success) this.allSeries = data
      })
    },
    // 查看置换订单
    handleDetails(row) {
      const { id } = row
      this.$router.push({
        path: '/orderManagement/deatailReplacementOrder',
        query: { id }
      })
    },
    // 查询
    search(form) {
      if (form.swopTime && form.swopTime.length > 0) {
        form.swopTimeStart = form.swopTime[0]
        form.swopTimeEnd = form.swopTime[1]
      } else {
        form.swopTimeStart = ''
        form.swopTimeEnd = ''
      }
      delete form.swopTime
      this.searchOptions = form
      this.currentPage = 1
      this.setTableData()
    },
    handleDataExport() {

    },
    // 导出
    excelInfo() {
      exportExcel('ZhOrder', '置换订单', { ...this.form }, this.pageSize, this.currentPage)
    }
  },
  async mounted() {
    await getAllBrand().then((res) => {
      const { success, data } = res
      if (success) this.allBrand = data
    })
  }
}
</script>
